<template>
  <a-layout-header class="header">
    <div class="logo">乾哥知识库</div>
    <a-menu
        v-model:selectedKeys="selectedKeys"
        theme="dark"
        mode="horizontal"
        :style="{ lineHeight: '64px' }"
    >
      <a-menu-item key="/">
        <router-link to="/">首页</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/user" :style="user.id ? {} : { display: 'none' }">
        <router-link to="/admin/user">用户管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/ebook" :style="user.id ? {} : { display: 'none' }">
        <router-link to="/admin/ebook">电子书管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/category" :style="user.id ? {} : { display: 'none' }">
        <router-link to="/admin/category">分类管理</router-link>
      </a-menu-item>
      <a-menu-item key="/about">
        <router-link to="/about">关于我们</router-link>
      </a-menu-item>
      <div class="login-menu">
        <span v-show="user.id">你好：{{user.name}}</span>
      </div>
      <a-popconfirm
          title="确认退出登录？"
          ok-text="是"
          cancel-text="否"
          @confirm="logout"
      >
        <a class="login-out">
          <span v-show="user.id">退出登录</span>
        </a>
      </a-popconfirm>
      <a class="login-menu" @click="showLoginModal" v-show="!user.id">
        <span>登录</span>
      </a>
    </a-menu>
    <a-modal v-model:visible="loginModalVisible" title="登录" :confirm-loading="loginConfirmLoading" @ok="handleLoginOk">
      <a-form
          :model="loginUser"
          :label-col="{ span: 8 }"
          :wrapper-col="{ span: 16 }"
          autocomplete="off"
      >
        <a-form-item label="登录名">
          <a-input v-model:value="loginUser.loginName"/>
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model:value="loginUser.password"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-layout-header>
</template>

<script lang="ts">
import {computed, defineComponent } from 'vue';
import { ref } from 'vue';
import axios from "axios";
import {message} from "ant-design-vue";
import store from '@/store';

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'theHeader',
  setup() {
    // 登录后监听登录成功的用户
    const user = computed(() => store.state.user);
    // 登录用户
    const loginUser = ref({
      loginName: 'test',
      password: '123'
    });

    const selectedKeys = ref<string[]>(['/']);

    // 弹出登录框
    const loginModalVisible = ref(false);
    const loginConfirmLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    }

    // 登录确认
    const handleLoginOk = () => {
      console.log("login...");
      loginConfirmLoading.value = true;
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      axios.post('/user/login', loginUser.value).then((response) => {
        loginConfirmLoading.value = false;
        const data = response.data;
        if (data.success) {
          // 将用户信息放入store
          store.commit('setUser', data.content);
          loginModalVisible.value = false;
          message.success("登录成功");
        } else {
          message.error(data.message);
        }
      })
    }

    // 退出登录
    const logout = () => {
      axios.get('/user/logout/' + user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          // 将空用户信息放入store
          store.commit('setUser', {});
          message.success("退出登录成功");
        } else {
          message.error(data.message);
        }
      })
    }

    return {
      selectedKeys,
      showLoginModal,
      loginModalVisible,
      loginUser,
      handleLoginOk,
      loginConfirmLoading,
      user,
      logout
    }
  }
});
</script>

<style scoped>
.login-menu {
  color: white;
  position: absolute;
  right: 150px;
}
.login-out {
  color: white;
  position: absolute;
  right: 50px;
}
.logo {
  width: 120px;
  height: 31px;
  /*background: rgba(255, 255, 255, 0.2);*/
  /*margin: 16px 28px 16px 0;*/
  float: left;
  color: white;
  font-size: 18px;
}
</style>